package com.wx.sys.service;

import com.wx.sys.mapper.SysRoleRescMapper;
import com.wx.sys.model.SysResc;
import com.wx.sys.model.biz.SysRescView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by king-pc on 2018/12/4.
 */
@Service
public class SysRoleRescService {

    @Autowired
    private SysRoleRescMapper sysRoleRescMapper;

    /**
     * 查询角色菜单
     * @param roleId
     * @return
     */
    public List<SysRescView> getRoleRescList(String roleId){
        List<SysResc> list = sysRoleRescMapper.getRoleRescList(roleId);

        List<SysRescView> rsList = new ArrayList<SysRescView>();

        //一级菜单
        SysRescView rescView = new SysRescView();
        String id = null;
        for(int i=0;i<list.size();i++){
            SysResc userResc = list.get(i);
            if("-1".equals(userResc.getParentId())){
                id = userResc.getId();

                rescView.setId(userResc.getId());
                rescView.setName(userResc.getName());
                rescView.setUri(userResc.getUri());
                rescView.setCss(userResc.getCss());

                list.remove(i);
                i--;


                //二级菜单
                List<SysResc> rescList = new ArrayList<SysResc>();
                for(int n=0;n<list.size();n++){
                    SysResc userResc2 = list.get(n);
                    if(id.equals(userResc2.getParentId())){
                        SysResc sysResc = new SysResc();
                        sysResc.setId(userResc2.getId());
                        sysResc.setName(userResc2.getName());
                        sysResc.setUri(userResc2.getUri());
                        sysResc.setCss(userResc2.getCss());

                        rescList.add(sysResc);
                        list.remove(n);
                        n--;
                    }
                }
                if(rescList.size()>0){
                    rescView.setList(rescList);
                }
                rsList.add(rescView);
                rescView = new SysRescView();
            }
        }
        return rsList;
    }
}
